"""
@Project    : cosmo-face
@Module     : train_retina.py
@Author     : HuangJiWen[huangjiwen@haier.com]
@Created    : 2020/8/16 21:30
@Desc       : 训练widerface
"""

from __future__ import print_function

import argparse
import datetime
import os
import random
import time

import math
import torch
import torch.backends.cudnn as cudnn
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data as data

from data import WiderFaceDetection, detection_collate, preproc
from data import cfg_retina_mobilenet, cfg_retina_slim, cfg_retina_rfb, cfg_retina_ghost, cfg_retina_resnet50
from layers.functions.prior_box import PriorBox
from layers.modules import MultiBoxLoss
from models.retina_face.ghostnet_face import RetinaGhostNetFace
from models.retina_face.mobilenet_resnet50_face import RetinaMobileRes50NetFace
from models.retina_face.rfbnet_face import RetinaRFBNetFace
from models.retina_face.slimnet_face import RetinaSlimNetFace


def train(args, cfg):

    # bgr order
    rgb_mean = (104, 117, 123)
    num_classes = 2
    img_dim = cfg['image_size']
    batch_size = cfg['batch_size']
    max_epoch = cfg['epoch']

    net = None
    if args.network == "mobilenet0.25" or args.network == "resnet50":
        net = RetinaMobileRes50NetFace(cfg=cfg)
    elif args.network == "slim":
        net = RetinaSlimNetFace(cfg=cfg)
    elif args.network == "RFB":
        net = RetinaRFBNetFace(cfg=cfg)
    elif args.network == "ghost":
        net = RetinaGhostNetFace(cfg=cfg)
    else:
        print("Don't support network!")
        exit(0)

    print("Printing net...")
    print(net)

    if args.resume_net is not None:
        print('Loading resume network...')
        state_dict = torch.load(args.resume_net)
        # create new OrderedDict that does not contain `module.`
        from collections import OrderedDict
        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            head = k[:7]
            if head == 'module.':
                name = k[7:]  # remove `module.`
            else:
                name = k
            new_state_dict[name] = v
        net.load_state_dict(new_state_dict)

    if torch.cuda.is_available():
        if cfg['ngpu'] > 1 and cfg['gpu_train']:
            net = torch.nn.DataParallel(net).cuda()
        else:
            net = net.cuda()

        cudnn.benchmark = True

    optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
    criterion = MultiBoxLoss(num_classes=num_classes, overlap_thresh=0.35, prior_for_matching=True, bkg_label=0,
                             neg_mining=True, neg_pos=7, neg_overlap=0.35, encode_target=False)

    net.train()
    epoch = 0 + args.resume_epoch
    print('Loading Dataset...')

    dataset = WiderFaceDetection(txt_path=args.training_dataset, preproc=preproc(img_dim, rgb_mean), img_dim=img_dim)

    epoch_size = math.ceil(len(dataset) / batch_size)
    max_iter = max_epoch * epoch_size

    step_values = (cfg['decay1'] * epoch_size, cfg['decay2'] * epoch_size)
    step_index = 0

    if args.resume_epoch > 0:
        start_iter = args.resume_epoch * epoch_size
    else:
        start_iter = 0

    for iteration in range(start_iter, max_iter):

        if iteration % epoch_size == 0:
            # create batch iterator
            batch_iterator = iter(data.DataLoader(dataset, batch_size, shuffle=True, num_workers=args.num_workers,
                                                  collate_fn=detection_collate))
            if (epoch % 10 == 0 and epoch > 0) or (epoch % 5 == 0 and epoch > cfg['decay1']):
                torch.save(net.state_dict(), args.save_folder + cfg['name'] + '_epoch_' + str(epoch) + '.pth')
            epoch += 1

        load_t0 = time.time()
        if iteration in step_values:
            step_index += 1

        lr = adjust_learning_rate(optimizer=optimizer, initial_lr=args.lr, gamma=args.gamma, epoch=epoch,
                                  step_index=step_index, iteration=iteration, epoch_size=epoch_size)

        # load train data
        images, targets = next(batch_iterator)
        images = images.float()

        # Multi-scale
        if args.multi_scale:
            sz = random.randrange(int(img_dim * 0.8), int(img_dim * 1.2) + 32) // 32 * 32  # size
            sf = sz / max(images.shape[2:])  # scale factor
            if sf != 1:
                ns = [math.ceil(x * sf / 32) * 32 for x in images.shape[2:]]  # new shape (stretched to gs-multiple)
                images = F.interpolate(images, size=ns, mode='bilinear', align_corners=False)

        if torch.cuda.is_available():
            images = images.cuda()
            targets = [anno.cuda() for anno in targets]

        prior_box = PriorBox(cfg, image_size=(images.shape[2], images.shape[3]))
        with torch.no_grad():
            priors = prior_box.forward()
            if torch.cuda.is_available():
                priors = priors.cuda()

        # forward
        out = net(images)

        # back prop
        optimizer.zero_grad()
        loss_l, loss_c, loss_landmark = criterion(predictions=out, priors=priors, targets=targets)
        loss = cfg['loc_weight'] * loss_l + loss_c + loss_landmark
        loss.backward()
        optimizer.step()
        load_t1 = time.time()
        batch_time = load_t1 - load_t0
        eta = int(batch_time * (max_iter - iteration))
        print('Epoch:{}/{} || Epochiter: {}/{} || Iter: {}/{} || Loc: {:.4f} Cla: {:.4f} Landm: {:.4f} || LR: {:.8f} || Batchtime: {:.4f} s || ETA: {}'
              .format(epoch, max_epoch, (iteration % epoch_size) + 1, epoch_size, iteration + 1, max_iter,
                      loss_l.item(), loss_c.item(), loss_landmark.item(), lr, batch_time,
                      str(datetime.timedelta(seconds=eta))))

    torch.save(net.state_dict(), args.save_folder + cfg['name'] + '_Final.pth')


def adjust_learning_rate(optimizer, initial_lr, gamma, epoch, step_index, iteration, epoch_size):
    """Sets the learning rate
    # Adapted from PyTorch Imagenet example:
    # https://github.com/pytorch/examples/blob/master/imagenet/main.py
    """
    warmup_epoch = 5
    if epoch <= warmup_epoch:
        lr = 1e-6 + (initial_lr-1e-6) * iteration / (epoch_size * warmup_epoch)
    else:
        lr = initial_lr * (gamma ** step_index)
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr
    return lr


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Retinaface Training')
    parser.add_argument('--training_dataset',
                        default='./data/wider_face/train/label.txt', help='Training dataset directory')
    parser.add_argument('--network', default='resnet50',
                        help='Backbone network mobilenet0.25 or slim or RFB or ghost or resnet50')
    parser.add_argument('--num_workers', default=0, type=int, help='Number of workers used in dataloading')
    parser.add_argument('--lr', '--learning-rate', default=1e-3, type=float, help='initial learning rate')
    parser.add_argument('--momentum', default=0.9, type=float, help='momentum')
    parser.add_argument('--resume_net',
                        # default=None,
                        default="./weights/Resnet50_retina_Final.pth",
                        help='resume net for retraining')
    parser.add_argument('--resume_epoch', default=0, type=int, help='resume iter for retraining')
    parser.add_argument('--weight_decay', default=5e-4, type=float, help='Weight decay for SGD')
    parser.add_argument('--gamma', default=0.1, type=float, help='Gamma update for SGD')
    parser.add_argument('--save_folder', default='./weights/', help='Location to save checkpoint models')
    parser.add_argument('--multi_scale', default=True, help='Location to save checkpoint models')

    args = parser.parse_args()

    if not os.path.exists(args.save_folder):
        os.mkdir(args.save_folder)

    cfg = None
    if args.network == "mobilenet0.25":
        cfg = cfg_retina_mobilenet
    elif args.network == "resnet50":
        cfg = cfg_retina_resnet50
    elif args.network == "slim":
        cfg = cfg_retina_slim
    elif args.network == "RFB":
        cfg = cfg_retina_rfb
    elif args.network == "ghost":
        cfg = cfg_retina_ghost
    else:
        print("Don't support network!")
        exit(0)

    train(args, cfg)
